package com.njym.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.njym.annotation.PermInfo;
import com.njym.entity.SysPaymentConfig;
import com.njym.service.SysPaymentConfigService;
import com.njym.vo.Json;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 配置信息
 *
 * @author zhy
 * @create 2018-08-24 12:40
 **/
@PermInfo(value = "系统支付接口配置")
@RestController
@RequiresPermissions("a:sys:paymentConfig")
@RequestMapping("/sys_paymentConfig")
public class SysPaymentConfigController {
	private static final Logger log = LoggerFactory.getLogger(SysPaymentConfigController.class);

	@Autowired
	private SysPaymentConfigService sysPaymentConfigService;

	@PermInfo("查找配置信息")
	@RequiresPermissions("a:sys:paymentConfig:queryConfig")
	@PostMapping("queryConfig")
	public Json queryConfigBymodeId(@RequestBody String body) {
		String oper = "query payment info";
		log.info("{}, body: {}", oper, body);
		JSONObject json = JSON.parseObject(body);
		//渠道id
		String modeId = json.getString("modeId");
		//配置名称
		String configName = json.getString("configName");
		int current = json.getIntValue("current");
		int size = json.getIntValue("size");
		if (current == 0) current = 1;
		if (size == 0) size = 10;
		Wrapper<SysPaymentConfig> queryParams = new EntityWrapper<>();
		if(modeId!=null && StringUtils.isNotEmpty(modeId)){
			queryParams.eq("mode_id",modeId);
		}
		if(configName!=null && StringUtils.isNoneEmpty(configName)){
			queryParams.like("config_name",configName);
		}
		Page<SysPaymentConfig> page = sysPaymentConfigService.selectPage(new Page<>(current, size), queryParams);
		return Json.succ(oper).data("page", page);
	}

	@PermInfo("添加配置信息")
	@RequiresPermissions("a:sys:paymentConfig:addConfig")
	@PostMapping("addConfig")
	public Json addConfigValue(@RequestBody String body){
		String oper = "add payment congig";
		log.info("{}, body: {}",oper,body);
		SysPaymentConfig paymentConfig = JSON.parseObject(body, SysPaymentConfig.class);
		SysPaymentConfig paymentConfigDB = sysPaymentConfigService.selectOne(new EntityWrapper<SysPaymentConfig>().eq("config_name",paymentConfig.getConfigName() ).eq("deleted",0));
		if (paymentConfigDB != null) {
			return Json.fail(oper, "该配置已经建立");
		}
		paymentConfig.setDeleted(0);
		boolean success = sysPaymentConfigService.insert(paymentConfig);
		return Json.result(oper, success)
			.data("id",paymentConfig.getId());


	}
	@PermInfo("修改配置信息")
	@RequiresPermissions("a:sys:paymentConfig:updateConfig")
	@PostMapping("updateConfig")
	public Json updateConfigValue(@RequestBody String body) {
		String oper = "update payment info";
		log.info("{}, body: {}",oper,body);
		SysPaymentConfig paymentConfig = JSON.parseObject(body, SysPaymentConfig.class);
		paymentConfig.setDeleted(0);
		Boolean success = sysPaymentConfigService.updateById(paymentConfig);
		return Json.result(oper, success);
	}

	@PermInfo("逻辑删除配置")
	@RequiresPermissions("a:sys:paymentConfig:deleteConfig")
	@DeleteMapping
	public Json deleteConfigById(@RequestBody String body) {
		String oper = "delete payment config";
		log.info("{}, body: {}",oper,body);

		JSONObject jsonObj = JSON.parseObject(body);
		String id = jsonObj.getString("id");
		boolean success =  sysPaymentConfigService.deleteById(id);
		return Json.result(oper, success);
	}
}
